package com.lepu.app.fun.tool.std;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.main.UtilityBase;
import com.core.lib.widget.MyDialog;
import com.eyzhs.app.R;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewRightButtonListener;
import com.lib_fitchart.FitChart;

public class TaiDongActivity extends BaseFragmentActivity implements
		OnTopbarNewLeftLayoutListener , OnTopbarNewRightButtonListener{
	private final int TIME_COUNT = 3600; //秒
	
	private CustomTopBarNew topbar;
	private TextView mNumberText;
	private TextView mTimeText;
	private Button mButton;
	
	private static FitChart mFitChart;
	
	private int mMoveCount = 0;
	private MyCount myCount;  
	public static TaiDongActivity instance;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		mNeedStatusBar = false;
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tool_taidong_activity);

		UtilityBase.setStatusBarRes(this, R.color.color_taidong, true);
		instance = this;
		init();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		instance = null;
		myCount.cancel();
	}

	/**
	 * 添加页面动画，需要重写这个方法，
	 */
	@Override
	public void onBackPressed() {
		backRemind();
	}
	
	@Override
	public void onTopbarLeftLayoutSelected() {
		backRemind();
	}

	@Override
	public void onTopbarRightButtonSelected() {
		myCount.cancel();
		mFitChart.setValue(0);
		mTimeText.setText("60:00");
		mMoveCount = 0;
		mNumberText.setText(""+mMoveCount);
		mButton.setText("开始计时");
	}
	
	public static TaiDongActivity getInstance(){
		return instance;
	}
	
	/**
	 * 初始化方法
	 */
	private void init() {
		// topbar
		topbar = (CustomTopBarNew) findViewById(R.id.topbar);
		topbar.setTopbarTitle("数胎动");
		topbar.setRightText("复位");
		topbar.setTopbarBackground(R.color.color_taidong);
		topbar.setonTopbarNewLeftLayoutListener(this);
		topbar.setOnTopbarNewRightButtonListener(this);
		
		mNumberText = (TextView) findViewById(R.id.number);
		mTimeText = (TextView) findViewById(R.id.time);
		mButton = (Button) findViewById(R.id.button);
		
		mFitChart = (FitChart) findViewById(R.id.fitchart);
		
		mButton.setOnClickListener(onClick);
		
		mFitChart.setMinValue(0f);
		mFitChart.setMaxValue(60f);
		
		myCount = new MyCount(TIME_COUNT*1000, 1000); 
		
	}

     
	View.OnClickListener onClick = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.button:
				String text = mButton.getText().toString();
				if(text.equals("开始计时")){
					myCount.start();
					mButton.setText("动一下");
				}else if(text.equals("动一下")){
					mMoveCount++;
					mNumberText.setText(""+mMoveCount);
				}else if(text.equals("重新测量")){
					mMoveCount = 0;
					mNumberText.setText(""+mMoveCount);
					mFitChart.setValue(0);
					myCount.start();
					mButton.setText("动一下");
				}
				break;

			default:
				break;
			}
		}
	};
	
    //定义一个倒计时的内部类 
    class MyCount extends CountDownTimer {     
        public MyCount(long millisInFuture, long countDownInterval) {     
            super(millisInFuture, countDownInterval);     
        }     
        @Override     
        public void onFinish() {   
        	//finish
        	mButton.setText("重新测量");
        	mTimeText.setText("00:00");
        }     
        @Override     
        public void onTick(long millisUntilFinished) {
        	int second = (int)(millisUntilFinished/1000);
        	if(second == TIME_COUNT){
        		second = second-1;
        	}
        	mTimeText.setText(secToTime(second));     
        }    
    } 
    
    public void backRemind(){
    	if(mButton.getText().toString().equals("动一下")){
			new MyDialog(TaiDongActivity.this)
            .setMessage("是否放弃本次计数")
            .setNegativeButton(R.string.app_cancel,
                    new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {

                        }
                    })
            .setPositiveButton(R.string.app_ok,
                    new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                        	finish(true);
                        }
                    }).create(null).show();
    	}else{
    		finish(true);
    	}
    }
    
    public static String secToTime(int time) {  
        String timeStr = null;  
        int hour = 0;  
        int minute = 0;  
        int second = 0;  
        if (time <= 0)  
            return "00:00";  
        else {  
            minute = time / 60;  
            if (minute < 60) {  
                second = time % 60;  
                timeStr = unitFormat(minute) + ":" + unitFormat(second);  
            } else {  
                hour = minute / 60;  
                minute = minute % 60;  
                second = time - hour * 3600 - minute * 60;  
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);  
            }  
            
            mFitChart.setValue(60-minute);
            
        }  
        return timeStr;  
    }  
  
    public static String unitFormat(int i) {  
        String retStr = null;  
        if (i >= 0 && i < 10)  
            retStr = "0" + Integer.toString(i);  
        else  
            retStr = "" + i;  
        return retStr;  
    } 
    
	
}
